﻿using LxBasic;

namespace LxCore
{
    public partial class ZoneCFDFields
    {
        protected void CheckCreate_T()
        {
            if (PhysicalModel.IsEnergyEnabled)
            {
                switch (PhysicalModel.PhysicalType, PhysicalModel.MaterialProperty.ThermalAnisotropyType)
                {
                    case (PhysicalTypes.固体, AnisotropyTypes.各向同性):
                        CheckCreatAndReuse(ref Temperature, out PossionEquation newSolidT);
                        break;
                    case (PhysicalTypes.固体, AnisotropyTypes.各向异性):
                        CheckCreatAndReuse(ref Temperature, out AnisotropyDiffusion newAniT);
                        break;
                    case (PhysicalTypes.固体, AnisotropyTypes.正交各向异性):
                        CheckCreatAndReuse(ref Temperature, out OrthotropicDiffusion newOAniT);
                        break;
                    case (PhysicalTypes.流体, _):
                        CheckCreatAndReuse(ref Temperature, out NSEquation newFluidT);
                        break;
                }
                Temperature.CheckCreate(Zone);
            }
            else
            {
                Temperature = null;
            }
        }
        protected void SetParameter_T()
        {
            if (Temperature == null) return;
            (var matp, var refv, var iniv, var sols) = GetPhysicalModelDetail();
            Temperature.SetZone_CommonTerm(ZoneCond.EnergySource_b, ZoneCond.EnergySource_a, iniv.InitialTemperature, sols.EnergyRelaxFactor);
            switch (Temperature)
            {
                case PossionEquation pos:
                    pos.SetZone_DiffTerm(matp.ThermalConductivity);
                    break;
                case NSEquation ns:
                    ns.SetZone_DiffTerm(matp.ThermalConductivity);//TODO 湍流要改 (GammaCommon == null ? matp.ThermalConductivity : GammaCommon);
                    ns.SetZone_ConvTerm(matp.SpecificHeat, PhysicalModel.SolveSetting.EnergyConvScheme, MassFlux);
                    break;
                case AnisotropyDiffusion ani:
                    ani.SetZone_TransformMatrix(new Vector3d(1, 0, 0), new Vector3d(0, 1, 0));
                    ani.SetZone_DiffTerm(matp.k11, matp.k12, matp.k13,
                        matp.k21, matp.k22, matp.k23,
                        matp.k31, matp.k32, matp.k33);
                    break;
                case OrthotropicDiffusion ort:
                    ort.SetZone_TransformMatrix(new Vector3d(1, 0, 0), new Vector3d(0, 1, 0));
                    ort.SetZone_DiffTerm(matp.k11, matp.k22, matp.k33);
                    break;
            }
            if (LxSim.Sim.Physics.IsSteady)
            {
                Temperature.SetZone_TransTerm(false, null, null);
            }
            else
            {
                Temperature.SetZone_TransTerm(true, matp.SpecificHeat, LxSim.Sim.Definitions.SystemField.Density);
            }
            foreach (var bound in Zone.Bounds)
            {
                var bset = ResHelper.GetCond(bound);
                if (bset.IsCouplingEnabled)
                {
                    Temperature.SetBound_CoupledWall(ConstField.Double0, ConstField.Double0, bound);
                }
                else
                {
                    switch (bset.BoundType)
                    {
                        case BoundTypes.壁面:
                            switch (bset.WallThermalType)
                            {
                                case WallThermalTypes.定壁温:
                                    Temperature.SetBound_FixValueWall(bset.Temperature, bound);
                                    break;
                                case WallThermalTypes.定热流密度:
                                    if (DomRadiation == null)
                                    {
                                        Temperature.SetBound_FixFluxWall(bset.HeatFlux_b, bset.HeatFlux_a, bound);
                                    }
                                    else
                                    {
                                        //var combiner = new FieldCombiner.Double();
                                        //combiner.Add(bound.HeatFlux_b);
                                        //combiner.Add(DomRadiation.HeatSourceOfRadiation);
                                        Temperature.SetBound_FixFluxWall(bset.HeatFlux_b, bset.HeatFlux_a, bound);
                                    }
                                    break;
                                case WallThermalTypes.定换热系数:
                                    if (DomRadiation == null)
                                    {
                                        Temperature.SetBound_FixhTWall(bset.Coupledh, bset.CoupledT, bound);
                                    }
                                    else
                                    {
                                        throw new NotImplementedException();
                                        //T.SetBound_FluxCombinedWall(DomRadiation.HeatSourceOfRadiation, bound.Coupledh, bound.CoupledT, bound);
                                    }
                                    break;
                            }
                            break;
                        case BoundTypes.对称面:
                            if (DomRadiation == null)
                            {
                                Temperature.SetBound_Symmetry(bound);
                            }
                            else
                            {
                                LxLog.Error("辐射计算暂不支持Symmetry边界");
                            }
                            break;
                        case BoundTypes.速度入口:
                        case BoundTypes.压力出口:
                            (Temperature as NSEquation).SetBound_FlowValue(bset.Temperature, bound);
                            break;
                        default:
                            LxLog.Error("未知边界类型");
                            break;
                    }
                }


            }
        }
    }
}